//
//  ZXReadAddViewModel.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/8.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXReadAddViewModel.h"

#import "ZXRecomSubscripitionManager.h"

@interface ZXReadAddViewModel ()

// 保存模型list数组
@property (nonatomic,strong) NSMutableArray *modelArr;

@property (nonatomic, strong) NSMutableArray * allDatas;

@end

@implementation ZXReadAddViewModel

- (instancetype)init{
    self = [super init];
    if (self) {
        self.modelArr = [[NSMutableArray alloc] init];
    }
    return self;
}

// 网络加载数据
- (void)getDataCompletionHandle:(void (^)(NSError *))completed {
    self.dataTask = [ZXRecomSubscripitionManager getRecomSubscriptionpageId:_pageId WithIntroduceCompletionHandle:^(ZXReadAddModel* responseObject, NSError *error)  {
        self.model = responseObject;
        
        // 记录模型数组
        [self.modelArr addObjectsFromArray:responseObject.list];
        
        completed(error);
    }];
}



- (void)refreshDataCompletionHandle:(void (^)(NSError *))completed {
    // 默认第一次根据page=1加载出来的数据
    
    _pageId = 1;
    
    [self getDataCompletionHandle:completed];
    
}



- (void)getMoreDataCompletionHandle:(void (^)(NSError *))completed {
    // page+1加载出来的数据作为新值
    //    _pageId += 1;
    
    [self getDataCompletionHandle:completed];
}



- (NSInteger)listCount {
    
    return self.model.maxPageId;
}

// section是组数, index是按钮下标( 或者行标号)
// 图
- (NSURL *)coverURLForIndex:(NSInteger)index {
    NSString *path = nil;
    path =  self.model.list[index].albumCoverUrl290;
    return [NSURL URLWithString:path];
}

// 标题
- (NSString *)titleForIndex:(NSInteger)index {
    
    return self.model.list[index].title;
}

// 副标题
- (NSString *)lastUptrackTitleForIndex:(NSInteger)index {
    
    return self.model.list[index].lastUptrackTitle;
    
}

// 评分
- (NSInteger)scoreForIndex:(NSInteger)index {
    
    return self.model.list[index].score;
    
}

// 评论人数
- (NSInteger)commentsCountForIndex:(NSInteger)index {
    return self.model.list[index].commentsCount;
    
}

// 价格
- (NSString *)displayDiscountedPriceForIndex:(NSInteger)index {
    
    return self.model.list[index].displayPrice;
}

-(NSInteger)pageId{
    if (!_pageId) {
        _pageId =  1;
    }
    return _pageId;
}



@end
